package org.boulderfoodrescue.webapp.shared;

import java.io.Serializable;
import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;

public class OneTimeEvent implements Serializable, HasEventSummary{
	private static final long serialVersionUID = -6178383356773052831L;

	private Long databaseKey;

	/**
	 * The start time of the event
	 */
	private Date startTime;

	/**
	 * The end time of the event
	 */
	private Date endTime;

	/**
	 * If true then the event occurs from the start of the start date
	 * though the end date.
	 */
	private Boolean allDay;

	/**
	 * The Database Key Of the Location that is connected to this
	 * OneTimeEvent
	 */
	private Long locationKey;

	public OneTimeEvent(Date startTime, Date endTime, Boolean allDay)
	{
		this.startTime = startTime;
		this.endTime = endTime;
		this.allDay = allDay;
	}

	public OneTimeEvent()
	{
		allDay = false;
		//We auto set the start time to the current time
		startTime = new Date();
		//Start time plus one hour
		endTime = new Date(startTime.getTime() + 3600000);
	}

	public OneTimeEvent(Date current, long event_length, boolean allDay) {
		this(current, new Date(current.getTime()+event_length), allDay);
	}
	
	@Override
	public String getEventSummary() {
		StringBuilder sb = new StringBuilder();
		sb.append("On ");
		sb.append(DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT).format(startTime));
		sb.append(" between ");
		sb.append(getTimeRange());
		return sb.toString();
	}

	@Override
	public EventType getEventType() {
		return EventType.OneTime;
	}

	public String getTimeRange(){
		if(allDay)
			return "all day";
		StringBuilder sb = new StringBuilder(DateTimeFormat.getFormat("H:mm").format(startTime));
		sb.append(" to ");
		sb.append(DateTimeFormat.getFormat("H:mm").format(endTime));
		return sb.toString();
	}
	
	public Long getDatabaseKey() {
		return databaseKey;
	}

	public void setDatabaseKey(Long databaseKey) {
		this.databaseKey = databaseKey;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Boolean getAllDay() {
		return allDay;
	}

	public void setAllDay(Boolean allDay) {
		this.allDay = allDay;
	}

	public Long getLocationKey() {
		return locationKey;
	}

	public void setLocationKey(Long locationKey) {
		this.locationKey = locationKey;
	}
}
